//
//  MagazineViewController.m
//  HelpThePoor
//
//  Created by van7ish on 2016/12/1.
//  Copyright © 2016年 van7ish. All rights reserved.
//

#import "MagazineViewController.h"
#import "magazineTableViewCell.h"
#import "MagazineDetailViewController.h"

@interface MagazineViewController () <UITableViewDelegate, UITableViewDataSource>

/** 导航条 */
@property (nonatomic, strong) UIImageView * navBar;
/** 精选杂志列表 */
@property (nonatomic, strong) UITableView * magazineTableView;
/** 数据源 */
@property (nonatomic, strong) NSMutableArray * magazineArr;
/** 加载页数 */
@property (nonatomic, assign) NSInteger pageIndex;

@end

@implementation MagazineViewController

- (UIImageView *)navBar{
    if (!_navBar) {
        _navBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 64)];
        _navBar.image = ImageNamed(@"daohangtiao");
    }
    return _navBar;
}

- (NSMutableArray *)magazineArr{
    if (!_magazineArr) {
        _magazineArr = [NSMutableArray array];
    }
    return _magazineArr;
}

- (UITableView *)magazineTableView{
    if (!_magazineTableView) {
        _magazineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_W, SCREEN_H - 64)];
        _magazineTableView.delegate = self;
        _magazineTableView.dataSource = self;
        _magazineTableView.backgroundColor = COLOR_LIGHTBGCOLOR;
        _magazineTableView.rowHeight = SCREEN_W / 2.6;
        _magazineTableView.tableFooterView = [UIView new];
        _magazineTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _magazineTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestMagazine:)];
        _magazineTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMagazine:)];
    }
    return _magazineTableView;
}

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.navBar];
    
    [self.view addSubview:self.magazineTableView];
    
    [self.magazineTableView.mj_header beginRefreshing];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc{
    DLog(@"%s", __func__);
}

#pragma mark - UITableViewDelegate & UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.magazineArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"magazineCell";
    MagazineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MagazineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    cell.titleLabel.text = [NSString stringWithFormat:@"%@", self.magazineArr[indexPath.row][@"name"]];
    cell.descLabel.text = self.magazineArr[indexPath.row][@"summarytext"];

    cell.timeLabel.text = self.magazineArr[indexPath.row][@"publishTime"];
    
    NSString *coverimgpath = [NSString stringWithFormat:@"%@%@", KHostAddress, self.magazineArr[indexPath.row][@"coverimgpath"]];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:coverimgpath] placeholderImage:ImageNamed(@"placeholdershuji")];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DLog(@"index = %ld", indexPath.row);
    
    MagazineDetailViewController *magazineDetailVC = [[MagazineDetailViewController alloc] init];
    magazineDetailVC.magazineDic = self.magazineArr[indexPath.row];
    [self.navigationController pushViewController:magazineDetailVC animated:YES];
}

#pragma mark - data request
/** 精选杂志（期刊） */
- (void)requestMagazine:(UIView *)view{
    if ([view isKindOfClass:[MJRefreshNormalHeader class]]) {
        self.pageIndex = 1;
        [self.magazineArr removeAllObjects];
    } else {
        self.pageIndex += 1;
    }
    NSString *URL = [NSString stringWithFormat:KMagazine, KHostAddress, KOrganizationID, @(self.pageIndex)];
    [ZRRequestManager POST:URL parameters:nil success:^(id responseObject) {
        DLog(@"%@", responseObject);
        
        if ([[responseObject objectForKey:@"error"] integerValue] == 0) {
            
            [self.magazineArr addObjectsFromArray:responseObject[@"data"][@"items"]];
            [self.magazineTableView reloadData];
        }
        
        [self.magazineTableView.mj_header endRefreshing];
        [self.magazineTableView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {
        DLog(@"%@", error);
        [self.magazineTableView.mj_header endRefreshing];
        [self.magazineTableView.mj_footer endRefreshing];
    }];
}

@end
